package materials;

import hulppakket.Color3f;
import hulppakket.Intersection;
import hulppakket.Scene;
import hulppakket.Vector3f;
import lights.Light;

public class PhongMaterial extends Material {

	public float shininess;

	public PhongMaterial(Color3f color, float shininess, String name) {
		super(color, name);
		this.shininess = shininess;
	}

	@Override
	public Color3f getShadedColor(Scene scene, Intersection intersection) {
		Color3f color = new Color3f();
		Color3f colorTemp;
		Vector3f lightVector;
		for (Light light : scene.getCurrentLights()) {
			lightVector = light.getVectorPointingToLight(intersection);
			if (!scene.isBlocked(intersection, lightVector)) {
				Vector3f eyeVector = intersection.ray.getDirection().scale(-1);
				Vector3f h = eyeVector.add(lightVector).normalize();
				float dot = h.dotProduct(intersection.getNormal());
				dot = (float) Math.pow(dot, shininess);
				colorTemp = new Color3f(this.color.multiply(light
						.getIntensifiedColor()));
				colorTemp = colorTemp.scale(dot);
				colorTemp = colorTemp.clamp();
				color = color.add(colorTemp).clamp();
			}
		}
		return color;
	}
}
